package com.letian.web.controller.record;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.record.domain.RecordTime;
import com.letian.record.service.IRecordTimeService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 记录时间Controller
 *
 * @author bmz
 * @date 2023-01-15
 */
@RestController
@RequestMapping("/record/time")
public class RecordTimeController extends BaseController {
    @Autowired
    private IRecordTimeService recordTimeService;

    /**
     * 查询记录时间列表
     */
    @PreAuthorize("@ss.hasPermi('record:time:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordTime recordTime) {
        startPage();
        List<RecordTime> list = recordTimeService.selectRecordTimeList(recordTime);
        return getDataTable(list);
    }

    /**
     * 查询记录时间列表
     */
    @PreAuthorize("@ss.hasPermi('record:time:list')")
    @GetMapping("/my/list")
    public TableDataInfo myList(RecordTime recordTime) {
        startPage();
        recordTime.setOwner(getUsername());
        List<RecordTime> list = recordTimeService.selectRecordTimeList(recordTime);
        return getDataTable(list);
    }

    /**
     * 查询记录时间列表
     */
    @PreAuthorize("@ss.hasPermi('record:time:list')")
    @GetMapping("/list/today")
    public TableDataInfo listToday(RecordTime recordTime) {
        startPage();
        recordTime.setOwner(getUsername());
        List<RecordTime> list = recordTimeService.selectRecordTimeListToday(recordTime);
        return getDataTable(list);
    }

    /**
     * 导出记录时间列表
     */
    @PreAuthorize("@ss.hasPermi('record:time:export')")
    @Log(title = "记录时间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordTime recordTime) {
        List<RecordTime> list = recordTimeService.selectRecordTimeList(recordTime);
        ExcelUtil<RecordTime> util = new ExcelUtil<RecordTime>(RecordTime.class);
        util.exportExcel(response, list, "记录时间数据");
    }

    /**
     * 获取记录时间详细信息
     */
    @PreAuthorize("@ss.hasPermi('record:time:query')")
    @GetMapping(value = "/{timeId}")
    public AjaxResult getInfo(@PathVariable("timeId") Long timeId) {
        return success(recordTimeService.selectRecordTimeByTimeId(timeId));
    }

    /**
     * 新增记录时间
     */
    @PreAuthorize("@ss.hasPermi('record:time:add')")
    @Log(title = "记录时间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordTime recordTime) {
        recordTime.setOwner(getUsername());
        recordTime.setCreateBy(getUsername());
        return toAjax(recordTimeService.insertRecordTime(recordTime));
    }

    /**
     * 修改记录时间
     */
    @PreAuthorize("@ss.hasPermi('record:time:edit')")
    @Log(title = "记录时间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordTime recordTime) {
        recordTime.setUpdateBy(getUsername());
        Long count = recordTime.getUpdateCount() == null ? 1 : recordTime.getUpdateCount() + 1;
        recordTime.setUpdateCount(count);
        return toAjax(recordTimeService.updateRecordTime(recordTime));
    }

    /**
     * 删除记录时间
     */
    @PreAuthorize("@ss.hasPermi('record:time:remove')")
    @Log(title = "记录时间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{timeIds}")
    public AjaxResult remove(@PathVariable Long[] timeIds) {
        return toAjax(recordTimeService.deleteRecordTimeByTimeIds(timeIds));
    }
}
